<?php
	class models_mathang extends connect{
		function listMatHang(){
			$sql = "SELECT MaMH, MaLH, TenMH, DVT, SoLuong, Gia, HinhAnh, MoTa
					FROM mathang";
			$this->query_db($sql);
			return $this->fetchAll();
		}
		
		function addMatHang($MaLH, $TenMH, $DVT,$SoLuong, $Gia, $HinhAnh, $MoTa){
			$sql = "INSERT INTO mathang VALUES ('','$MaLH','$TenMH','$DVT','$SoLuong','$Gia', '$HinhAnh','$MoTa')";
			$this->query_db($sql);
			if(!$this->result){
				die (mysql_error($this->conn)); //Bat loi tu mysql trigger
				echo '<script> window.location.href="index.php?mod=addmathang"</script>';
			}
		}
		
		function delMatHang($intMa){
			$sql = "DELETE
					FROM mathang
					WHERE MaMH = '$intMa'";
			$this->query_db($sql);	
		}
		function getMatHang($intMa)
		{
			$sql = "SELECT MaMH, MaLH, TenMH, DVT, SoLuong
					FROM mathang 
					WHERE MaMH = '$intMa'";
			
			$this->query_db($sql);
			return $this->fetchOne();
		}
		function updateMatHang($MaLH, $TenMH, $DVT, $SoLuong,$intMa, $Gia, $HinhAnh, $MoTa)
		{
			$sql ="	UPDATE mathang
					SET MaLH = '$MaLH', TenMH = '$TenMH', DVT = '$DVT', SoLuong = '$SoLuong', Gia='$Gia', HinhAnh='$HinhAnh', MoTa='$MoTa'
					WHERE MaMH = '$intMa'";
			$this->query_db($sql);
		}
		function _tongsodong(){
			$sql='SELECT COUNT(MaLH)
				  FROM loaihang';
			$this->query_db($sql);
			return $this->totalRow();
		}
	}
?>